<?php

// +----------------------------------------------------------------------
// |仓库服务层
// +----------------------------------------------------------------------

namespace app\common\system;

use think\facade\Validate;
use think\facade\Db;
use think\facade\Cache;
use app\common\Model\ConfigModel;

class ConfigService
{

      /**
     * 获取系统配置的列表
     * @param array $param 前台传过来的参数  
     *   
      */

      public static function list($param){
     
        $model= new ConfigModel();
        $where =$data= [];
        $params= $param['params']; 
        if($params['name']){
            $where[] =['name','like',"%".$params['name']."%"];
        }
        if($params['code']){
          $where[] =['code','like',"%".$params['code']."%"];
        }
        $pk = $model->getPk();      
        $field = $pk . ',name,code,storage_id,values';          
        $config_list = $model->field($field)->where($where)->order('id asc')->select()->toArray();         

        $data['list']=$config_list;
        $data['total']=(int) count($config_list);     
        return $data;
     }

       /**
     * 设置添加与修改
     * @param array $param 前台传过来的参数  
     *   
      */
      public static function create($param){
        $model= new ConfigModel();
        $params= $param['params'];
        $pk = $model->getPk(); 
        $data=[
            'code' => $params['code'],
            'name' => $params['name'],   
        ];    
        if(empty($params['storage_id'])){
             $data['storage_id'] = $params['storage_id'];
        }
    
        $keyValue = $params['keyValue'];

        foreach($keyValue as $k=> $v){
            //如果没有键，只有值的判断
            if(empty($v['keyName'])){					
                $keyValues[] = $v['valueName'];
                $data['values']  = implode(',',$keyValues);
            }else{
                $keyValues[$v['keyName']] = $v['valueName'];
                $data['values']  = json_encode($keyValues,JSON_UNESCAPED_UNICODE);
            }          
        } 
      
        if($params[$pk]){
            //有值则说明是修改     
            $result =  $model::find($params[$pk])->save($data);
        }else{        
                $result =  $model->save($data);
        }
        if($result){
            return true;
        }else{
            error('修改失败');
        }     


      }

     /**
     * 获取系统配置单条数据
     * @param array $param 前台传过来的参数  
     *   
      */
    public static function get_list($param){
        $model= new ConfigModel();
        $params= $param['params']; 
        $pk = $model->getPk(); 
        $configList =$model->where($pk,$params[$pk])->find();

        $valueList  =  $configList['values'];
        $brackets   = "{";
        if(strstr($valueList,$brackets)){
            //检测出来是json格式还是字符串
            $valueLists = json_decode($valueList);
            foreach($valueLists as $k=>$v){
                $valueListss[] = [
                    'keyName'   => $k,
                    'valueName' => $v,
                ];
            }
        }else{
            $valueLists = explode(',',$valueList);
            foreach($valueLists as $k=>$v){
                $valueListss[] = [
                    'keyName'   => '',
                    'valueName' => $v,
                ];
            }
        }
        $data['list'] = $valueListss;
        return $data;
    }


     /**
     * 获取单位数据
     * @param array $param 前台传过来的参数  
     *   
      */
    public static function measures($param){
        $data=Db::name('measures')->select()->toArray();
        return $data;
    }

      /**
     * 获取售后模板数据
     * @param array $param 前台传过来的参数  
     *   
      */
    public static function aftersale_template($param){
       $data=Db::name('aftersale_template')->select()->toArray();
       return $data;  
    }
    
      /**
     * 获取国家列表数据
     * @param array $param 前台传过来的参数  
     *   
      */
    public static  function country_list($param){
        $params= $param['params'];
        $where=[];
        if($params['description']){
            $where[] = ['description','like','%'.$params['description'].'%'];
        }           
        $field = 'id,currency,name,chinaarea,vaild,countryid,description';   
        $result=Db::name('country')->where($where)->field($field)->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();
        $data['list']=$result['data'];
        $data['total']=$result['total'];  
        return $data; 
    }
}